package com.siebre.picclife.asynctask;

import java.util.List;

import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Lists;
import com.siebre.picclife.asynctask.model.Task;
import com.siebre.picclife.asynctask.model.TaskStatus;
import com.siebre.picclife.asynctask.service.TaskRepository;

class InMemoryTaskRepository implements TaskRepository {
	private List<Task> taskQueue = Lists.newLinkedList();

	public void save(Task task) {
		if (taskQueue.contains(task)) {
			return;
		}
		taskQueue.add(task);
	}

	public Task findOne(long id) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<Task> findByArgument(final String descriptor) {
		return FluentIterable.from(taskQueue).filter(new Predicate<Task>() {

			public boolean apply(Task input) {
				return input.getArgument().equals(descriptor);
			}
		}).toList();
	}

	public List<Task> findNewTasks(int pageIndex, int pageSize) {
		return FluentIterable.from(taskQueue)
				.skip(pageIndex * pageSize)
				.filter(new Predicate<Task>() {

					public boolean apply(Task input) {
						return input.getStatus().equals(TaskStatus.NEW);
					}
				})
				.limit(pageSize).toList();
	}

	public List<Task> findFailedTasks(int pageIndex, int pageSize) {
		return FluentIterable.from(taskQueue)
				.skip(pageIndex * pageSize)
				.filter(new Predicate<Task>() {

					public boolean apply(Task input) {
						return input.getStatus().equals(TaskStatus.FAILED);
					}
				})
				.limit(pageSize).toList();
	}

	public List<Task> findNewTasks() {
		return FluentIterable.from(taskQueue).filter(new Predicate<Task>() {

			public boolean apply(Task input) {
				return input.getStatus().equals(TaskStatus.NEW);
			}
		}).toList();
	}

	public List<Task> findFailedTasks() {
		return FluentIterable.from(taskQueue).filter(new Predicate<Task>() {

			public boolean apply(Task input) {
				return input.getStatus().equals(TaskStatus.FAILED);
			}
		}).toList();
	}

	public List<Task> findCorruptedTasks(int pageIndex, int pageSize) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<Task> findProcessingTasks(int pageIndex, int pageSize) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<Task> findProcessingTasks(String processor, int pageIndex,
			int pageSize) {
		// TODO Auto-generated method stub
		return null;
	}

	public void deleteAll() {
		// TODO Auto-generated method stub
		
	}
}